﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace scifistories
{
    /// <summary>
    /// Manager der sich um das Zeichnen aller Objekte kümmert
    /// </summary>
    public class DrawingManager
    {
        private static SpriteBatch spriteBatch;
        private static List<IDrawableGameObject> objects;

        static DrawingManager()
        {
            objects = new List<IDrawableGameObject>();
        }

        /// <summary>
        /// Initialisiert den Manager
        /// </summary>
        /// <param name="_spriteBatch">Eine SpriteBatch</param>
        public static void Initialize(SpriteBatch _spriteBatch)
        {
            spriteBatch = _spriteBatch;
        }

        /// <summary>
        /// Fügt ein Objekt zur Liste der zu zeichnenden Objekte hinzu
        /// </summary>
        /// <param name="dObject">Das Objekt</param>
        public static void RegisterObject(IDrawableGameObject dObject)
        {
            objects.Add(dObject);
        }

        /// <summary>
        /// Zeichnet alle Objekte
        /// </summary>
        public static void Draw()
        {
            spriteBatch.Begin();
            foreach (IDrawableGameObject dObject in objects)
            {
                spriteBatch.Draw(dObject.Texture, dObject.ObjectRectangle,
                    dObject.SourceRectangle, Color.White, dObject.Rotation,
                    dObject.Origin, dObject.Effects, 0);
            }
            spriteBatch.End();
        }

        /// <summary>
        /// Zeichnet alle Objekte auf der Liste
        /// </summary>
        /// <param name="dObjects">Liste mit den zu zeichnenden Objekten</param>
        public static void Draw(List<IDrawableGameObject> dObjects)
        {
            spriteBatch.Begin();
            foreach (IDrawableGameObject dObject in dObjects)
            {
                spriteBatch.Draw(dObject.Texture, dObject.ObjectRectangle,
                    dObject.SourceRectangle, Color.White, dObject.Rotation,
                    dObject.Origin, dObject.Effects, 0);
            }
            spriteBatch.End();
        }

        /// <summary>
        /// Zeichnet ein einzelnes Objekt
        /// </summary>
        /// <param name="dObject">Das Objekt</param>
        public static void Draw(IDrawableGameObject dObject)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(dObject.Texture, dObject.ObjectRectangle,
                dObject.SourceRectangle, Color.White, dObject.Rotation, dObject.Origin,
                dObject.Effects, 0);
            spriteBatch.End();
        }

        public static void DrawString(SpriteFont font, string text, Vector2 position, Color color)
        {
            spriteBatch.Begin();
            spriteBatch.DrawString(font, text, position, color);
            spriteBatch.End();
        }
    }
}
